<?php

declare(strict_types=1);

namespace App\Entities;

use App\Utils\Tool;
use Illuminate\Support\Facades\Log;

class OrderEntity extends ABaseEntity
{
    public function __construct(OrderModel $model)
    {
        $this->model = $model;
    }

    /**
     * 根据order_channel表主键查询订单是否存在
     *
     * @return static|null
     */
    public static function findOrderEntityByOrderChannelId(int $order_channelid): ?self
    {
        $model_order = OrderModel::where('order_channelid', $order_channelid)->first();
        if (empty($model_order)) {
            Log::error(sprintf('订单不存在，order_channelid:%d', $order_channelid));

            return null;
        }

        return new self($model_order);
    }

    /**
     * @param string $tradeNo
     * @return OrderEntity|null
     */
    public static function findByTradeNo(string $tradeNo)
    {
        $model = OrderModel::where('no', $tradeNo)->first();
        if ($model === null) {
            return null;
        }

        return new self($model);
    }

    /**
     * @return OrderEntity|null
     */
    public static function findByGid(string $gid)
    {
        $model = OrderModel::where('gid', $gid)->first();
        if ($model === null) {
            return null;
        }

        return new self($model);
    }

    public function getOrderChannelid(): int
    {
        return (int) $this->model->order_channelid;
    }

    public function setOrderChannelid(int $order_channelid): void
    {
        $this->model->order_channelid = $order_channelid;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return (int) $this->model->id;
    }

    /**
     * 订单号
     *
     * @return string
     */
    public function getNo()
    {
        return $this->model->no;
    }

    /**
     * @param string $no
     */
    public function setNo(string $no): void
    {
        $this->model->no = $no;
    }

    /**
     * @return string
     */
    public function getPaidAt()
    {
        return (string) $this->model->paid_at;
    }

    public function setPaidAt(string $val)
    {
        $this->model->paid_at = $val;
    }

    /**
     * @return bool
     */
    public function isPaid()
    {
        return empty($this->getPaidAt()) ? false : true;
    }

    /**
     * @return string
     */
    public function getPaymentMethod()
    {
        return (string) $this->model->payment_method;
    }

    /**
     * @param string $val
     */
    public function setPaymentMethod($val)
    {
        $this->model->payment_method = $val;
    }

    /**
     * @return string
     */
    public function getPaymentNo()
    {
        return $this->model->payment_no ?: '';
    }

    /**
     * @param string $val
     */
    public function setPaymentNo($val)
    {
        $this->model->payment_no = $val;
    }

    /**
     * @return string
     */
    public function getShipStatus()
    {
        return $this->model->ship_status;
    }

    /**
     * @param string $val
     */
    public function setShipStatus($val)
    {
        $this->model->ship_status = $val;
    }

    /**
     * @return string
     */
    public function getRefundStatus()
    {
        return $this->model->refund_status;
    }

    /**
     * @param string $val
     */
    public function setRefundStatus($val)
    {
        $this->model->refund_status = $val;
    }

    /**
     * @return int
     */
    public function getClosed()
    {
        return $this->model->closed;
    }

    /**
     * @param int $val
     */
    public function setClosed($val)
    {
        $this->model->closed = $val;
    }

    /**
     * @return int
     */
    public function getProductUnitId()
    {
        return $this->model->product_unit_id;
    }

    /**
     * @param int $val
     */
    public function setProductUnitId($val)
    {
        $this->model->product_unit_id = $val;
    }

    /**
     * @return int
     */
    public function getSkuShowType()
    {
        return $this->model->sku_show_type;
    }

    /**
     * @param int $val
     */
    public function setSkuShowType($val)
    {
        $this->model->sku_show_type = $val;
    }

    /**
     * 订单是否已经评价
     *
     * @return bool
     */
    public function reviewed()
    {
        return $this->model->reviewed;
    }

    /**
     * 订单是否已经评价
     *
     * @param int $val
     */
    public function setReviewed($val)
    {
        $this->model->reviewed = $val;
    }

    public function setTotalAmount($totalAmount)
    {
        $this->model->total_amount = $totalAmount;
    }

    /**
     * 订单金额
     *
     * @return float
     */
    public function getTotalAmount()
    {
        return $this->model->total_amount;
    }

    /**
     * 订单创建时间
     *
     * @return string
     */
    public function getCreatedAt()
    {
        return $this->model->created_at;
    }

    /**
     * OA 订单表主键
     *
     * @return int
     */
    public function getGid()
    {
        return $this->model->gid ?: 0;
    }

    /**
     * OA系统 ID
     *
     * @return int
     */
    public function getOaGid()
    {
        return $this->model->oa_gid ?: 0;
    }

    /**
     * 服务地址
     */
    public function setFullAddress(string $fullAddress)
    {
        $this->model->full_address = $fullAddress;
    }

    /**
     * 服务地址
     */
    public function getFullAddress(): string
    {
        return $this->model->full_address;
    }

    /**
     * 城市ID
     *
     * @return int
     */
    public function getCityId()
    {
        return $this->model->city_id;
    }

    /**
     * 城市ID
     */
    public function setCityId(int $cityId)
    {
        $this->model->city_id = $cityId;
    }

    /**
     * 用户姓名
     */
    public function setContactName(string $contactName)
    {
        $this->model->contact_name = $contactName;
    }

    /**
     * 用户姓名
     */
    public function getContactName(): string
    {
        return $this->model->contact_name;
    }

    public function getSafeContactName(): string
    {
        return mb_substr($this->getContactName(), 0, 1) . '**';
    }

    /**
     * 用户联系电话
     */
    public function setContactPhone(string $contactPhone)
    {
        $this->model->contact_phone = $contactPhone;
    }

    /**
     * 用户联系电话
     */
    public function getContactPhone(): string
    {
        return $this->model->contact_phone;
    }

    public function getSafeContactPhone(): string
    {
        return substr_replace($this->getContactPhone(), '****', 3, 4);
    }

    /**
     * 用户备注
     *
     * @return string
     */
    public function getRemark()
    {
        return (string) $this->model->remark;
    }

    /**
     * 用户备注
     */
    public function setRemark(string $remark)
    {
        $this->model->remark = $remark;
    }

    /**
     * 获取卖家备注
     */
    public function getSellerRemark(): string
    {
        return (string) $this->model->seller_remark;
    }

    /**
     * 设置卖家备注
     */
    public function setSellerRemark(string $seller_remark): void
    {
        $this->model->seller_remark = $seller_remark;
    }

    public function setCompanyId(int $id)
    {
        $this->model->company_id = $id;
    }

    /**
     * @return int
     */
    public function getCompanyId()
    {
        return $this->model->company_id;
    }

    /**
     * 下单日期 Y-m-d
     */
    public function setAssignDate(string $assignDate)
    {
        $this->model->assignDate = $assignDate;
    }

    /**
     * 下单日期 Y-m-d
     *
     * @return string
     */
    public function getAssignDate()
    {
        return $this->model->assignDate;
    }

    /**
     * 下单时间段 ID
     */
    public function setAssignTime(int $assignTime)
    {
        $this->model->assignTime = $assignTime;
    }

    /**
     * @return int
     */
    public function getAssignTime()
    {
        return $this->model->assignTime;
    }

    /**
     * 用户ID
     *
     * @return int
     */
    public function getUserId()
    {
        return $this->model->user_id;
    }

    /**
     * 用户ID
     */
    public function setUserId(int $userId)
    {
        $this->model->user_id = $userId;
    }

    /**
     * 获取保洁员名名字
     *
     * @return string
     */
    public function getBnames()
    {
        return (string) $this->model->bnames;
    }

    public function setBnames(string $val)
    {
        $this->model->bnames = $val;
    }

    /**
     * 服务完成时间
     *
     * @return string
     */
    public function getCompletionTime()
    {
        $time = $this->model->completion_time ?: 0;
        if ($time) {
            return date('Y-m-d H:i:s', $time);
        } else {
            return '';
        }
    }

    /**
     * 订单使用的优惠券码
     */
    public function setCouponCodeId(int $id)
    {
        $this->model->coupon_code_id = $id;
    }

    /**
     * 订单使用的优惠券码
     *
     * @return int
     */
    public function getCouponCodeId()
    {
        return $this->model->coupon_code_id ?: 0;
    }

    /**
     * @return int
     */
    public function getTransactionRecordId()
    {
        return $this->model->transaction_record_id ?: 0;
    }

    /**
     * @return void
     */
    public function setTransactionRecordId(int $id)
    {
        $this->model->transaction_record_id = $id;
    }

    /**
     * @return int
     */
    public function getOrderState()
    {
        return $this->model->order_state;
    }

    public function setOrderState(int $state)
    {
        $this->model->order_state = $state;
    }

    public function setBids(int $val)
    {
        $this->model->bids = $val;
    }

    /**
     * @return int
     */
    public function getBids()
    {
        return (int) $this->model->bids;
    }

    /**
     * 计划上门时间
     */
    public function setPlanServiceTime(int $planservicetime)
    {
        $this->model->planservicetime = $planservicetime;
    }

    /**
     * @return int
     */
    public function getPlanServiceTime()
    {
        return $this->model->planservicetime;
    }

    /**
     * 设置计划上门时间点
     */
    public function setTimePeriod(int $timeperiod)
    {
        $this->model->timeperiod = $timeperiod;
    }

    /**
     * @return int
     */
    public function getTimePeriod()
    {
        return $this->model->timeperiod;
    }

    /**
     * 获取sku的title
     *
     * @return string
     */
    public function getSkuTitle()
    {
        return $this->model->sku_title ?: '';
    }

    /**
     * 设置sku的title
     */
    public function setSkuTitle(string $skuTitle)
    {
        $this->model->sku_title = $skuTitle;
    }

    public function getSyncStatus(): int
    {
        return $this->model->sync_status;
    }

    public function setSyncStatus(int $sync_status): void
    {
        $this->model->sync_status = $sync_status;
    }

    public function setGid(int $gid): void
    {
        $this->model->gid = $gid;
    }

    public function setOaGid(string $oa_gid): void
    {
        $this->model->oa_gid = $oa_gid;
    }

    public function setProductId(int $productId)
    {
        $this->model->product_id = $productId;
    }

    /**
     * @return int
     */
    public function getProductId()
    {
        return $this->model->product_id;
    }

    public function setProductSkuId(int $productSkuId)
    {
        $this->model->product_sku_id = $productSkuId;
    }

    /**
     * @return int
     */
    public function getProductSkuId()
    {
        return $this->model->product_sku_id;
    }

    /**
     * @return int
     */
    public function isAdminImport()
    {
        return $this->model->is_admin_import;
    }

    /**
     * @param  int
     */
    public function setIsAdminImport(int $value)
    {
        $this->model->is_admin_import = $value;
    }

    /*
 * @return string
 */
    public function getChangeDateReason(): string
    {
        return $this->model->change_date_reason;
    }

    /*
     * @param string
     */
    public function setChangeDateReason(string $reasonText): void
    {
        $this->model->change_date_reason = $reasonText;
    }

    public function getChannel(): string
    {
        return (string) $this->model->channel;
    }

    public function setChannel(string $channel): void
    {
        $this->model->channel = $channel;
    }

    public function getChannelId(): int
    {
        return (int)$this->model->channel_id;
    }

    public function setChannelId(int $channelId): void
    {
        $this->model->channel_id = $channelId;
    }

    /**
     * 获取预约时间
     */
    public function getBookDate()
    {
        return $this->getAssignDate() . ' ' . Tool::periodCode2StrNew((string)$this->getAssignTime(), true);
    }
}
